package com.lh.tams.util.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.lh.tams.exception.BusinessException;

public class SqlTemplate {
	public String sql;

	public SqlTemplate(String sql) {
		this.sql = sql;
	}

	/**
	 * 根据sql获得ResultSet对象
	 * 
	 * @param sql
	 *            sql查询语句
	 * @return ResultSet对象
	 */
	public Object execute(CallBack callBack) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		ConnUtil connUtil = null;
		try {
			connUtil = ConnUtil.newInstance();// 获得连接工具
			con = connUtil.getCon();
			System.err.println("连接数据库成功...");

			stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);//设置可滚动的结果集
			rs = stmt.executeQuery(sql);
			return callBack.export(rs);// 留给回调函数去处理
		} catch (Exception e) {
			throw new BusinessException("导入失败，异常信息:"+e.getMessage());
		} finally {
			ConnUtil.closeCon(rs, stmt, con);
		}
	}

}
